package com.xnyzc.lhy.mis.util.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xnyzc.lhy.common.penum.push.EJPushBalanceChangeContent;
import com.xnyzc.lhy.common.penum.push.EJPushBalanceChangeType;
import com.xnyzc.lhy.common.penum.push.EJPushConfigKey;
import com.xnyzc.lhy.common.penum.push.EPushCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.mis.util.service.JPushBalanceChangeUtilService;
import com.xnyzc.lhy.mis.entity.user.OaCUser;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.feign.FeignMessageSendService;
import com.xnyzc.lhy.mis.mapper.user.OaCUserMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.resource.entity.jpush.BizResult;
import com.xnyzc.lhy.resource.entity.jpush.PushInfoEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 极光推送 余额变动推送
 * @Author zhuyubin
 * @Date 2019-09-24 14:38
 **/
@Slf4j
@Service
public class JPushBalanceChangeUtilServiceImpl implements JPushBalanceChangeUtilService {

    @Autowired
    private FeignMessageSendService feignMessageSendService;
    @Autowired
    private OaCUserMapper oaCUserMapper;
    @Autowired
    private OaDUserMapper oaDUserMapper;

    /**
     * 极光推送 乘客余额变动
     * @param userId 乘客id
     * @param tag 用户来源
     * @param type 余额变动状态 0充值 1消费
     * @param recordId 账单详情id
     * @param money 变动金额
     */
    @Override
    public void passengerPush(Long userId, String tag, Integer type, Long recordId, BigDecimal money){
        OaCUser oaCUser = oaCUserMapper.selectById(userId);
        if (CheckUtil.objIsNotEmpty(oaCUser)){
            this.push(oaCUser.getUserPhone(), recordId, type, money, tag, EJPushConfigKey.passenger);
        }
    }

    /**
     * 极光推送 司机余额变动
     * @param driver 司机id
     * @param tag 用户来源
     * @param recordId 账单详情id
     * @param money 变动金额
     */
    @Override
    public void driverPush(Long driver, String tag, Long recordId, BigDecimal money){
        OaDUser oaDUser = oaDUserMapper.selectById(driver);
        if (CheckUtil.objIsNotEmpty(oaDUser)){
            this.push(oaDUser.getDriverPhone(), recordId, EJPushBalanceChangeType.recharge.getValue(), money, tag, EJPushConfigKey.driver);
        }
    }

    /**
     * 余额变动推送公共方法
     * @param phone
     * @param recordId
     * @param type
     * @param money
     */
    private void push(String phone, Long recordId, Integer type, BigDecimal money, String tag, EJPushConfigKey ejPushConfigKey){
        // 手机号们
        List<String> phones = new ArrayList<>();
        phones.add(phone);
        JSONObject jsonObject = new JSONObject();
        JSONObject result = new JSONObject();
        result.put("recordId", recordId.toString());
        result.put("type", type);
        jsonObject.put("expand", JSON.toJSONString(result));
        PushInfoEntity pushInfoEntity = new PushInfoEntity();
        pushInfoEntity.setExtras(jsonObject);
        pushInfoEntity.setPhones(phones);
        pushInfoEntity.setClientId(ejPushConfigKey.getValue());
        pushInfoEntity.setTag(tag);
        if (EJPushBalanceChangeType.consumption.getValue().equals(type)){
            pushInfoEntity.setPushCode(EPushCode.consumptionBalance.getValue());
            pushInfoEntity.setContent(String.format(EJPushBalanceChangeContent.consumption.getDesc(), money));
        } else {
            pushInfoEntity.setPushCode(EPushCode.rechargeBalance.getValue());
        }
        BizResult bizResult = feignMessageSendService.pushMessage(pushInfoEntity);

        log.info("{}", bizResult.toString());

        if (bizResult.getCode() == 1) {
            log.info("余额变更推送成功");
        } else {
            log.info("余额变更推送失败");
        }
    }
}
